/*
 * Tencent is pleased to support the open source community by making ovCompose available.
 * Copyright (C) 2025 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tencent.compose.video

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Star
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.tencent.compose.sample.backhandler.BackHandler

// 视频数据模型
data class VideoItem(
    val id: String,
    val title: String,
    val subtitle: String,
    val episodeInfo: String,
    val isFree: Boolean = false,
    val isNew: Boolean = false,
    val isExclusive: Boolean = false,
    val isFinale: Boolean = false,
    val isReserved: Boolean = false,
    val rating: Float? = null,
    val totalEpisodes: Int = 0,
    val currentEpisodes: Int = 0,
    val imageUrl: String,
    val videoUrl: String? = null, // 视频URL，如果提供则显示视频而不是图片
    val discussionCount: String = "",
    val watchCount: String = "",
    val heatCount: String = "",
    val collectionCount: String = ""
)

// 底部导航项
enum class BottomNavItem(val label: String, val icon: androidx.compose.ui.graphics.vector.ImageVector) {
    HOME("首页", Icons.Default.PlayArrow),
    SHORT_VIDEO("短视频", Icons.Default.Favorite),
    VIP("VIP会员", Icons.Default.Star),
    MINE("我的", Icons.Default.Favorite)
}

// 硬编码的视频数据 - 使用网络图片URL
private val videoItems = listOf(
    VideoItem(
        id = "1",
        title = "四喜·活宝爹妈",
        subtitle = "薛甄珠终于加上闺女微信了",
        episodeInfo = "全36集",
        isFinale = true,
        imageUrl = "https://picsum.photos/400/300?random=1",
        videoUrl = "https://media.w3.org/2010/05/sintel/trailer.mp4", // 第一个使用视频
        discussionCount = "讨论破100万"
    ),
    VideoItem(
        id = "2",
        title = "出发3加更·看进去了",
        subtitle = "沈腾过山车差点玩飞出去",
        episodeInfo = "更新至11-24期",
        isFree = true,
        imageUrl = "https://picsum.photos/400/300?random=2",
        watchCount = "在追破300万"
    ),
    VideoItem(
        id = "3",
        title = "offer番外・社死现场",
        subtitle = "帅气港弟被佘诗曼当场纠错",
        episodeInfo = "全10期",
        isExclusive = true,
        imageUrl = "https://picsum.photos/400/300?random=3"
    ),
    VideoItem(
        id = "4",
        title = "依然单身·点映看结局",
        subtitle = "反怼催婚就像霍建华这样",
        episodeInfo = "全16集",
        isFinale = true,
        imageUrl = "https://picsum.photos/400/300?random=4",
        heatCount = "实时热度超2.1万"
    ),
    VideoItem(
        id = "5",
        title = "死神来了6.胆小勿入",
        subtitle = "惊悚悬疑大片",
        episodeInfo = "",
        isNew = true,
        rating = 8.5f,
        imageUrl = "https://picsum.photos/400/300?random=5"
    ),
    VideoItem(
        id = "6",
        title = "100道古代美食·陈晓…",
        subtitle = "传统美食制作",
        episodeInfo = "11月27日上线",
        isReserved = true,
        imageUrl = "https://picsum.photos/400/300?random=6"
    ),
    VideoItem(
        id = "7",
        title = "令人心动的offer 第7季",
        subtitle = "陈铭 时代少年团贺峻霖 职场节目",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=7",
        collectionCount = "49万收藏",
        rating = 9.4f
    ),
    VideoItem(
        id = "8",
        title = "一见你就笑",
        subtitle = "杨迪 杨超越 游戏节目 推理游戏",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=8",
        collectionCount = "63万收藏"
    )
)

// 分类标签
private val categories = listOf("首页", "电视剧", "动漫", "电影", "综艺", "少儿")

// 横幅数据
private val bannerData = listOf(
    BannerItem(
        title = "四喜",
        subtitle = "四喜 | 薛甄珠终于加上闺女微信了",
        imageUrl = "https://picsum.photos/800/400?random=10",
        tag = "全网独播"
    )
)

// 大图卡片数据（一行一列的大图效果）
private val largeVideoCards = listOf(
    VideoItem(
        id = "large1",
        title = "令人心动的offer 第7季",
        subtitle = "陈铭 时代少年团贺峻霖 职场节目",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=20",
        collectionCount = "49万收藏",
        discussionCount = "讨论破300万",
        watchCount = "职场综艺榜第1名"
    ),
    VideoItem(
        id = "large2",
        title = "一见你就笑",
        subtitle = "杨迪 杨超越 游戏节目 推理游戏",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=21",
        collectionCount = "63万收藏"
    ),
    VideoItem(
        id = "large3",
        title = "专治恋爱脑的歌",
        subtitle = "治愈系音乐节目 情感共鸣",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=22",
        collectionCount = "28万收藏",
        discussionCount = "讨论破150万"
    ),
    VideoItem(
        id = "large4",
        title = "奔跑吧兄弟 第12季",
        subtitle = "邓超 陈赫 李晨 综艺真人秀",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=23",
        collectionCount = "85万收藏",
        watchCount = "综艺榜第2名"
    ),
    VideoItem(
        id = "large5",
        title = "极限挑战 第9季",
        subtitle = "黄渤 孙红雷 张艺兴 挑战极限",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=24",
        collectionCount = "72万收藏",
        heatCount = "实时热度超5.2万"
    ),
    VideoItem(
        id = "large6",
        title = "向往的生活 第8季",
        subtitle = "何炅 黄磊 慢生活综艺",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=25",
        collectionCount = "56万收藏"
    ),
    VideoItem(
        id = "large7",
        title = "中国好声音 第15季",
        subtitle = "周杰伦 那英 音乐选秀",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=26",
        collectionCount = "91万收藏",
        discussionCount = "讨论破500万"
    ),
    VideoItem(
        id = "large8",
        title = "这就是街舞 第6季",
        subtitle = "易烊千玺 韩庚 街舞竞技",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=27",
        collectionCount = "67万收藏",
        watchCount = "舞蹈综艺榜第1名"
    ),
    VideoItem(
        id = "large9",
        title = "明星大侦探 第9季",
        subtitle = "何炅 撒贝宁 推理悬疑",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=28",
        collectionCount = "78万收藏",
        heatCount = "实时热度超3.8万"
    ),
    VideoItem(
        id = "large10",
        title = "创造营2024",
        subtitle = "选秀综艺 青春励志",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=29",
        collectionCount = "94万收藏",
        discussionCount = "讨论破600万"
    ),
    VideoItem(
        id = "large11",
        title = "乘风破浪的姐姐 第5季",
        subtitle = "30+女星 成团竞演",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=30",
        collectionCount = "88万收藏",
        watchCount = "女性综艺榜第1名"
    ),
    VideoItem(
        id = "large12",
        title = "披荆斩棘的哥哥 第4季",
        subtitle = "30+男星 成团竞演",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=31",
        collectionCount = "76万收藏"
    ),
    VideoItem(
        id = "large13",
        title = "王牌对王牌 第8季",
        subtitle = "沈腾 贾玲 搞笑综艺",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=32",
        collectionCount = "82万收藏",
        heatCount = "实时热度超4.5万"
    ),
    VideoItem(
        id = "large14",
        title = "青春环游记 第5季",
        subtitle = "旅行真人秀 青春活力",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=33",
        collectionCount = "59万收藏"
    ),
    VideoItem(
        id = "large15",
        title = "中餐厅 第8季",
        subtitle = "美食经营 文化传播",
        episodeInfo = "",
        imageUrl = "https://picsum.photos/800/450?random=34",
        collectionCount = "65万收藏",
        discussionCount = "讨论破200万"
    )
)

data class BannerItem(
    val title: String,
    val subtitle: String,
    val imageUrl: String,
    val tag: String
)

@Composable
internal fun VideoHomePage() {
    var selectedVideo: VideoItem? by remember { mutableStateOf(null) }
    var selectedCategory by remember { mutableStateOf("首页") }
    var selectedBottomNav by remember { mutableStateOf(BottomNavItem.HOME) }
    var searchText by remember { mutableStateOf("枭起青壤") }

    BackHandler(selectedVideo != null) {
        selectedVideo = null
    }

    Column(modifier = Modifier.fillMaxSize()) {
        AnimatedContent(
            targetState = selectedVideo,
            transitionSpec = {
                val slideDirection =
                    if (targetState != null) AnimatedContentTransitionScope.SlideDirection.Start
                    else AnimatedContentTransitionScope.SlideDirection.End

                slideIntoContainer(
                    animationSpec = tween(250, easing = FastOutSlowInEasing),
                    towards = slideDirection
                ) + fadeIn(animationSpec = tween(250)) togetherWith
                        slideOutOfContainer(
                            animationSpec = tween(250, easing = FastOutSlowInEasing),
                            towards = slideDirection
                        ) + fadeOut(animationSpec = tween(250))
            },
            modifier = Modifier.weight(1f)
        ) { target ->
            if (target == null) {
                LazyColumn(
                    modifier = Modifier.fillMaxSize()
                ) {
                    // 搜索栏和菜单
                    item {
                        HeaderSection(
                            searchText = searchText,
                            onSearchTextChange = { searchText = it },
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(horizontal = 16.dp, vertical = 8.dp)
                        )
                    }

                    // 分类导航
                    item {
                        CategoryTabs(
                            categories = categories,
                            selectedCategory = selectedCategory,
                            onCategorySelected = { selectedCategory = it },
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(vertical = 8.dp)
                        )
                    }

                    // 文本推荐
                    item {
                        TextRecommendation(
                            text = "韩国高分电影解说《小委托人》:等姐姐长大,一定给你…",
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(horizontal = 16.dp, vertical = 4.dp)
                        )
                    }

                    // 横幅区域
                    item {
                        BannerSection(
                            banner = bannerData[0],
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(200.dp)
                                .padding(horizontal = 16.dp, vertical = 8.dp)
                        )
                    }

                    // 视频网格
                    item {
                        VideoGrid(
                            videos = videoItems,
                            onVideoClick = { video -> selectedVideo = video },
                            modifier = Modifier.fillMaxWidth()
                        )
                    }

                    // 大图卡片列表（一行一列的大图效果）
                    items(largeVideoCards) { video ->
                        LargeVideoCard(
                            video = video,
                            onClick = { selectedVideo = video },
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(horizontal = 16.dp, vertical = 8.dp)
                        )
                    }
                }
            } else {
                VideoDetailPage(
                    video = target,
                    onBack = { selectedVideo = null }
                )
            }
        }

        // 底部导航栏
        BottomNavigationBar(
            selectedItem = selectedBottomNav,
            onItemSelected = { selectedBottomNav = it },
            modifier = Modifier.fillMaxWidth()
        )
    }
}

@Composable
private fun HeaderSection(
    searchText: String,
    onSearchTextChange: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        // 汉堡菜单
        Icon(
            imageVector = Icons.Default.Menu,
            contentDescription = "菜单",
            modifier = Modifier
                .size(24.dp)
                .clickable { }
        )

        // 搜索栏
        Box(
            modifier = Modifier
                .weight(1f)
                .height(40.dp)
                .padding(horizontal = 12.dp)
                .clip(RoundedCornerShape(20.dp))
                .background(
                    color = Color(0xFFF5F5F5),
                    shape = RoundedCornerShape(20.dp)
                )
                .clickable { }
        ) {
            Row(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    imageVector = Icons.Default.Search,
                    contentDescription = "搜索",
                    modifier = Modifier.size(20.dp),
                    tint = Color.Gray
                )
                Spacer(modifier = Modifier.width(8.dp))
                Text(
                    text = searchText,
                    fontSize = 14.sp,
                    color = Color.Gray
                )
            }
        }

        // 通知图标
        Box(
            modifier = Modifier
                .size(24.dp)
                .clickable { }
        ) {
            Icon(
                imageVector = Icons.Default.Notifications,
                contentDescription = "通知",
                modifier = Modifier.size(20.dp),
                tint = Color.Red
            )
            // 红点提示
            Box(
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .size(8.dp)
                    .clip(CircleShape)
                    .background(Color.Red, CircleShape)
            )
        }
    }
}

@Composable
private fun TextRecommendation(
    text: String,
    modifier: Modifier = Modifier
) {
    Text(
        text = text,
        fontSize = 14.sp,
        color = Color(0xFF333333),
        modifier = modifier.clickable { }
    )
}

@Composable
private fun CategoryTabs(
    categories: List<String>,
    selectedCategory: String,
    onCategorySelected: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    LazyRow(
        modifier = modifier,
        horizontalArrangement = Arrangement.spacedBy(16.dp),
        contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 16.dp)
    ) {
        items(categories) { category ->
            CategoryTab(
                text = category,
                isSelected = category == selectedCategory,
                onClick = { onCategorySelected(category) }
            )
        }
    }
}

@Composable
private fun CategoryTab(
    text: String,
    isSelected: Boolean,
    onClick: () -> Unit
) {
    Text(
        text = text,
        fontSize = 16.sp,
        fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,
        color = if (isSelected) Color(0xFFFF6600) else Color.Black,
        modifier = Modifier
            .clickable { onClick() }
            .padding(vertical = 8.dp)
    )
}

@Composable
private fun BannerSection(
    banner: BannerItem,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .clip(RoundedCornerShape(8.dp))
    ) {
        // 使用网络图片
        NetworkImage(
            imageUrl = banner.imageUrl,
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier.fillMaxSize()
        )

        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.Black.copy(alpha = 0.35f))
        )

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.SpaceBetween
        ) {
            // 标签
            Box(
                modifier = Modifier
                    .clip(RoundedCornerShape(4.dp))
                    .background(
                        color = Color(0xFFFF6600),
                        shape = RoundedCornerShape(4.dp)
                    )
                    .padding(horizontal = 8.dp, vertical = 4.dp)
                    .align(Alignment.End)
            ) {
                Text(
                    text = banner.tag,
                    fontSize = 12.sp,
                    color = Color.White,
                    fontWeight = FontWeight.Bold
                )
            }

            Spacer(modifier = Modifier.weight(1f))

            // 标题信息
            Column {
                Text(
                    text = banner.title,
                    fontSize = 24.sp,
                    color = Color.White,
                    fontWeight = FontWeight.Bold
                )
                Spacer(modifier = Modifier.height(4.dp))
                Text(
                    text = banner.subtitle,
                    fontSize = 14.sp,
                    color = Color.White.copy(alpha = 0.9f)
                )
            }
        }

        // 轮播指示器
        Row(
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(bottom = 8.dp),
            horizontalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            repeat(3) { index ->
                Box(
                    modifier = Modifier
                        .size(6.dp)
                        .clip(RoundedCornerShape(3.dp))
                        .background(
                            color = if (index == 0) Color.White else Color.White.copy(alpha = 0.5f),
                            shape = RoundedCornerShape(3.dp)
                        )
                )
            }
        }
    }
}

@Composable
private fun VideoGrid(
    videos: List<VideoItem>,
    onVideoClick: (VideoItem) -> Unit,
    modifier: Modifier = Modifier
) {
    Column(
        modifier = modifier.padding(horizontal = 16.dp, vertical = 8.dp)
    ) {
        videos.chunked(2).forEach { rowVideos ->
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                rowVideos.forEach { video ->
                    VideoCard(
                        video = video,
                        onClick = { onVideoClick(video) },
                        modifier = Modifier.weight(1f)
                    )
                }
                // 如果只有一行一个视频，添加空白占位
                if (rowVideos.size == 1) {
                    Spacer(modifier = Modifier.weight(1f))
                }
            }
            Spacer(modifier = Modifier.height(16.dp))
        }
    }
}

@Composable
private fun VideoCard(
    video: VideoItem,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .clip(RoundedCornerShape(8.dp))
            .clickable { onClick() }
    ) {
        Column {
            // 缩略图或视频
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .aspectRatio(16f / 9f)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.Black)
            ) {
                // 如果有videoUrl，使用Video组件；否则使用图片
                if (video.videoUrl != null) {
                    VideoPlayer(
                        modifier = Modifier.fillMaxSize(),
                        url = video.videoUrl
                    )
                } else {
                    NetworkImage(
                        imageUrl = video.imageUrl,
                        contentDescription = null,
                        contentScale = ContentScale.Crop,
                        modifier = Modifier.fillMaxSize()
                    )
                }

                // 标签
                when {
                    video.isFree -> {
                        TagBadge(
                            text = "限免中",
                            color = Color(0xFFFF6600),
                            modifier = Modifier.align(Alignment.TopEnd)
                        )
                    }
                    video.isNew -> {
                        TagBadge(
                            text = "上新",
                            color = Color(0xFFFF0000),
                            modifier = Modifier.align(Alignment.TopEnd)
                        )
                    }
                    video.isExclusive -> {
                        TagBadge(
                            text = "独播",
                            color = Color(0xFFFF6600),
                            modifier = Modifier.align(Alignment.TopEnd)
                        )
                    }
                    video.isFinale -> {
                        TagBadge(
                            text = "结局点映",
                            color = Color(0xFFFF6600),
                            modifier = Modifier.align(Alignment.TopEnd)
                        )
                    }
                    video.isReserved -> {
                        TagBadge(
                            text = "预约",
                            color = Color(0xFFFF6600),
                            modifier = Modifier.align(Alignment.TopEnd)
                        )
                    }
                }

                // 评分
                video.rating?.let { rating ->
                    Box(
                        modifier = Modifier
                            .align(Alignment.BottomEnd)
                            .clip(RoundedCornerShape(8.dp, 0.dp, 8.dp, 0.dp))
                            .background(
                                color = Color.Black.copy(alpha = 0.7f),
                                shape = RoundedCornerShape(8.dp, 0.dp, 8.dp, 0.dp)
                            )
                            .padding(horizontal = 6.dp, vertical = 2.dp)
                    ) {
                        Text(
                            text = "$rating",
                            fontSize = 12.sp,
                            color = Color.White,
                            fontWeight = FontWeight.Bold
                        )
                    }
                }

                // 底部信息
                Column(
                    modifier = Modifier
                        .align(Alignment.BottomStart)
                        .fillMaxWidth()
                        .background(Color.Black.copy(alpha = 0.6f))
                        .padding(horizontal = 8.dp, vertical = 4.dp)
                ) {
                    if (video.episodeInfo.isNotEmpty()) {
                        Text(
                            text = video.episodeInfo,
                            fontSize = 11.sp,
                            color = Color.White
                        )
                    }
                    if (video.discussionCount.isNotEmpty()) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                imageVector = Icons.Default.Favorite,
                                contentDescription = null,
                                modifier = Modifier.size(12.dp),
                                tint = Color.White
                            )
                            Spacer(modifier = Modifier.width(4.dp))
                            Text(
                                text = video.discussionCount,
                                fontSize = 11.sp,
                                color = Color.White
                            )
                        }
                    }
                    if (video.watchCount.isNotEmpty()) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                imageVector = Icons.Default.PlayArrow,
                                contentDescription = null,
                                modifier = Modifier.size(12.dp),
                                tint = Color.White
                            )
                            Spacer(modifier = Modifier.width(4.dp))
                            Text(
                                text = video.watchCount,
                                fontSize = 11.sp,
                                color = Color.White
                            )
                        }
                    }
                    if (video.heatCount.isNotEmpty()) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                imageVector = Icons.Default.Star,
                                contentDescription = null,
                                modifier = Modifier.size(12.dp),
                                tint = Color.White
                            )
                            Spacer(modifier = Modifier.width(4.dp))
                            Text(
                                text = video.heatCount,
                                fontSize = 11.sp,
                                color = Color.White
                            )
                        }
                    }
                }
            }

            Spacer(modifier = Modifier.height(8.dp))

            // 标题
            Text(
                text = video.title,
                fontSize = 14.sp,
                color = Color.Black,
                fontWeight = FontWeight.Medium,
                maxLines = 1
            )

            // 副标题
            if (video.subtitle.isNotEmpty()) {
                Spacer(modifier = Modifier.height(4.dp))
                Text(
                    text = video.subtitle,
                    fontSize = 12.sp,
                    color = Color.Gray,
                    maxLines = 1
                )
            }

            // 收藏数等
            if (video.collectionCount.isNotEmpty()) {
                Spacer(modifier = Modifier.height(4.dp))
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        imageVector = Icons.Default.Star,
                        contentDescription = null,
                        modifier = Modifier.size(14.dp),
                        tint = Color(0xFFFF6600)
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(
                        text = video.collectionCount,
                        fontSize = 12.sp,
                        color = Color.Gray
                    )
                }
            }
        }
    }
}

@Composable
private fun TagBadge(
    text: String,
    color: Color,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .clip(RoundedCornerShape(0.dp, 8.dp, 0.dp, 8.dp))
            .background(
                color = color,
                shape = RoundedCornerShape(0.dp, 8.dp, 0.dp, 8.dp)
            )
            .padding(horizontal = 6.dp, vertical = 2.dp)
    ) {
        Text(
            text = text,
            fontSize = 10.sp,
            color = Color.White,
            fontWeight = FontWeight.Bold
        )
    }
}

@Composable
private fun LargeVideoCard(
    video: VideoItem,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .clip(RoundedCornerShape(8.dp))
            .clickable { onClick() }
    ) {
        Column {
            // 大图
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .aspectRatio(16f / 9f)
                    .clip(RoundedCornerShape(8.dp))
                    .background(Color.LightGray)
            ) {
                NetworkImage(
                    imageUrl = video.imageUrl,
                    contentDescription = null,
                    contentScale = ContentScale.Crop,
                    modifier = Modifier.fillMaxSize()
                )
            }

            Spacer(modifier = Modifier.height(12.dp))

            // 标题和收藏
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.Top
            ) {
                // 标题
                Column(modifier = Modifier.weight(1f)) {
                    Text(
                        text = video.title,
                        fontSize = 18.sp,
                        fontWeight = FontWeight.Bold,
                        color = Color.Black
                    )
                }

                // 收藏（右侧）
                if (video.collectionCount.isNotEmpty()) {
                    Column(horizontalAlignment = Alignment.End) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                imageVector = Icons.Default.Star,
                                contentDescription = null,
                                modifier = Modifier.size(16.dp),
                                tint = Color(0xFFFF6600)
                            )
                            Spacer(modifier = Modifier.width(4.dp))
                            Text(
                                text = "收藏",
                                fontSize = 14.sp,
                                color = Color.Black
                            )
                        }
                        Spacer(modifier = Modifier.height(4.dp))
                        Text(
                            text = video.collectionCount,
                            fontSize = 12.sp,
                            color = Color.Gray
                        )
                    }
                }
            }

            Spacer(modifier = Modifier.height(8.dp))

            // 副标题
            if (video.subtitle.isNotEmpty()) {
                Text(
                    text = video.subtitle,
                    fontSize = 14.sp,
                    color = Color.Gray
                )
                Spacer(modifier = Modifier.height(12.dp))
            }

            // 标签按钮
            Row(
                horizontalArrangement = Arrangement.spacedBy(8.dp),
                modifier = Modifier.fillMaxWidth()
            ) {
                if (video.watchCount.isNotEmpty()) {
                    Box(
                        modifier = Modifier
                            .clip(RoundedCornerShape(4.dp))
                            .background(
                                color = Color(0xFF0066FF),
                                shape = RoundedCornerShape(4.dp)
                            )
                            .padding(horizontal = 12.dp, vertical = 6.dp)
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Text(
                                text = video.watchCount,
                                fontSize = 12.sp,
                                color = Color.White,
                                fontWeight = FontWeight.Medium
                            )
                            Spacer(modifier = Modifier.width(4.dp))
                            Text(
                                text = ">",
                                fontSize = 12.sp,
                                color = Color.White
                            )
                        }
                    }
                }

                if (video.discussionCount.isNotEmpty()) {
                    Box(
                        modifier = Modifier
                            .clip(RoundedCornerShape(4.dp))
                            .background(
                                color = Color.Transparent,
                                shape = RoundedCornerShape(4.dp)
                            )
                            .border(1.dp, Color(0xFFE0E0E0), RoundedCornerShape(4.dp))
                            .padding(horizontal = 12.dp, vertical = 6.dp)
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Icon(
                                imageVector = Icons.Default.Favorite,
                                contentDescription = null,
                                modifier = Modifier.size(14.dp),
                                tint = Color.Black
                            )
                            Spacer(modifier = Modifier.width(4.dp))
                            Text(
                                text = video.discussionCount,
                                fontSize = 12.sp,
                                color = Color.Black,
                                fontWeight = FontWeight.Medium
                            )
                            Spacer(modifier = Modifier.width(4.dp))
                            Text(
                                text = ">",
                                fontSize = 12.sp,
                                color = Color.Black
                            )
                        }
                    }
                }
            }
        }
    }
}

@Composable
private fun BottomNavigationBar(
    selectedItem: BottomNavItem,
    onItemSelected: (BottomNavItem) -> Unit,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier
            .height(60.dp)
            .background(Color.White)
            .padding(horizontal = 16.dp),
        horizontalArrangement = Arrangement.SpaceAround,
        verticalAlignment = Alignment.CenterVertically
    ) {
        BottomNavItem.values().forEach { item ->
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .clickable { onItemSelected(item) }
                    .padding(8.dp)
            ) {
                Icon(
                    imageVector = item.icon,
                    contentDescription = item.label,
                    modifier = Modifier.size(24.dp),
                    tint = if (selectedItem == item) Color(0xFFFF6600) else Color.Gray
                )
                Spacer(modifier = Modifier.height(4.dp))
                Text(
                    text = item.label,
                    fontSize = 12.sp,
                    color = if (selectedItem == item) Color(0xFFFF6600) else Color.Gray,
                    fontWeight = if (selectedItem == item) FontWeight.Bold else FontWeight.Normal
                )
            }
        }
    }
}

